//
//  ChangePsdViewController.m
//  CommunityApp
//
//  Created by 秦乾龙 on 16/12/19.
//  Copyright © 2016年 秦乾龙. All rights reserved.
//

#import "ChangePsdViewController.h"
#import "NavView.h"
#import "MyTagView.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScale   ([UIScreen mainScreen].bounds.size.width)/750

@interface ChangePsdViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) NSArray *dataAray;
@property (nonatomic,strong) MyTagView *tagView;
@property (nonatomic,retain) NSDictionary *allDic;
@property (nonatomic,retain) UITextField *phoneField;
@property (nonatomic,retain) UITextField *codeField;
@property (nonatomic,retain) UIButton *codeBtn;
@property (nonatomic,retain) UITextField *psdField;
@property (nonatomic,retain) UITextField *surePsdField;
@end

@implementation ChangePsdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"修改密码"];
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backBtn;
    ;
    UIButton *buttonNotice = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonNotice.frame = CGRectMake(0, 0, 40, 20);
    buttonNotice.titleLabel.font = [UIFont systemFontOfSize:15];
    [buttonNotice setTitle:@"确定" forState:UIControlStateNormal];
    
    UIBarButtonItem *btnNoticeItem = [[UIBarButtonItem alloc]initWithCustomView:buttonNotice];
    [buttonNotice addTarget:self action:@selector(changeClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = btnNoticeItem;

    self.view.backgroundColor = [UIColor colorWithRed:32/255.0 green:32/255.0 blue:32/255.0 alpha:1.0f];
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    backgroundView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0f];
    [self.view addSubview:backgroundView];
    UIView *phoneView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 100*kScale)];
    phoneView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:phoneView];
    _phoneField = [[UITextField alloc]init];
    _phoneField.delegate = self;
    _phoneField.font = [UIFont systemFontOfSize:15];
    _phoneField.placeholder = @"请输入手机号码";
    _phoneField.frame = CGRectMake(30*kScale, 0, kScreenW, 100*kScale);
    _phoneField.keyboardType = UIKeyboardTypePhonePad;
    [phoneView addSubview:_phoneField];
    UIView *codeView = [[UIView alloc]initWithFrame:CGRectMake(0, 102*kScale, kScreenW, 100*kScale)];
    codeView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:codeView];
    _codeField = [[UITextField alloc]init];
    _codeField.delegate = self;
    _codeField.font = [UIFont systemFontOfSize:15];
    
    _codeField.placeholder = @"请输入验证码";
    _codeField.frame = CGRectMake(30*kScale, 0, 300*kScale, 100*kScale);
    _codeField.keyboardType = UIKeyboardTypePhonePad;
    [codeView addSubview:_codeField];
    CGFloat codeBtnW = 100;
    _codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _codeBtn.frame = CGRectMake(kScreenW-codeBtnW, 0, codeBtnW, 100*kScale);
    //    codeBtn setTitle:@"获取验证码" forState:ui
    [_codeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _codeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_codeBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
    [_codeBtn addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchUpInside];
    [codeView addSubview:_codeBtn];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(kScreenW-codeBtnW-20*kScale, 10*kScale, 6*kScale, 80*kScale)];
    lineView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0f];
    [codeView addSubview:lineView];
    UIView *newPsdView = [[UIView alloc]initWithFrame:CGRectMake(0, 204*kScale, kScreenW, 100*kScale)];
    newPsdView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:newPsdView];
    _psdField = [[UITextField alloc]init];
    _psdField.delegate = self;
    _psdField.font = [UIFont systemFontOfSize:15];
    
    _psdField.placeholder = @"请输入新密码";
    _psdField.frame = CGRectMake(30*kScale, 0, kScreenW-2*30*kScale-42*kScale - 20, 100*kScale);
    [newPsdView addSubview:_psdField];
    UIButton *eyeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    eyeBtn.frame = CGRectMake(kScreenW-30*kScale-52*kScale, 29*kScale, 52*kScale, 52*kScale);
    [eyeBtn addTarget:self action:@selector(eyeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [eyeBtn setImage:[UIImage imageNamed:@"眼"] forState:UIControlStateNormal];
    [eyeBtn setImage:[UIImage imageNamed:@"眼"] forState:UIControlStateSelected];
    _psdField.secureTextEntry = YES;
    [newPsdView addSubview:eyeBtn];
    UIView *surePsdView = [[UIView alloc]initWithFrame:CGRectMake(0, 306*kScale,kScreenW, 100*kScale)];
    surePsdView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:surePsdView];
    _surePsdField = [[UITextField alloc]init];
    _surePsdField.delegate = self;
    _surePsdField.font = [UIFont systemFontOfSize:15];
    _surePsdField.secureTextEntry = YES;
    _surePsdField.placeholder = @"确认输入新密码";
    _surePsdField.frame = CGRectMake(30*kScale, 0, kScreenW-2*30*kScale-42*kScale - 20, 100*kScale);
    [surePsdView addSubview:_surePsdField];
    UIButton *sureEyeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureEyeBtn.frame = CGRectMake(kScreenW-30*kScale-52*kScale, 29*kScale, 52*kScale, 52*kScale);
    [sureEyeBtn addTarget:self action:@selector(sureEyeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [sureEyeBtn setImage:[UIImage imageNamed:@"眼"] forState:UIControlStateNormal];
    [sureEyeBtn setImage:[UIImage imageNamed:@"眼"] forState:UIControlStateSelected];
    [surePsdView addSubview:sureEyeBtn];
}
- (void)backClick
{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)getCode:(id)sender
{
    //验证是否是手机号
    NSString *urlStr= [NSString stringWithFormat:@"%@/sys/login/memberRegistr/sms/randomCode?mobile_no=%@",APIYIGOU,_phoneField.text];
    
    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = urlStr;
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        _allDic = [NSDictionary changeType:request.responseJSONObject];
        NSLog(@"77777%@",_allDic);
        _codeField.text = _allDic[@"data"];
        [self timeRun];
        
    } error:^(YTKBaseRequest *request) {
        _allDic = [NSDictionary changeType:request.responseJSONObject];
        NSLog(@"77777%@",_allDic);
        [ViewFactory showInfoMsg:@"获取验证码失败"];
        
        
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
}

//倒计时
- (void)timeRun
{
    __block NSInteger time = 29; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮的样式
                [_codeBtn setTitle:@"重新发送" forState:UIControlStateNormal];
                [_codeBtn setTitleColor:[UIColor colorWithRed:171/255.0 green:147/255.0 blue:103/255.0 alpha:1.0f] forState:UIControlStateNormal];
                _codeBtn.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮显示读秒效果
                [_codeBtn setTitle:[NSString stringWithFormat:@"重新发送(%.2d)", seconds] forState:UIControlStateNormal];
                [_codeBtn setTitleColor:[UIColor colorWithRed:171/255.0 green:147/255.0 blue:103/255.0 alpha:1.0f] forState:UIControlStateNormal];
                _codeBtn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}

//密码显示
- (void)eyeBtnClick:(UIButton*)sender
{
    if (sender.selected==YES) {
        sender.selected = NO;
        _psdField.secureTextEntry = YES;
    }
    else
    {
        sender.selected = YES;
        _psdField.secureTextEntry = NO;
    }
}
- (void)sureEyeBtnClick:(UIButton*)sender
{
    if (sender.selected==YES) {
        sender.selected = NO;
        _surePsdField.secureTextEntry = YES;
    }
    else
    {
        sender.selected = YES;
        _surePsdField.secureTextEntry = NO;
    }
}

- (void) changeClick
{
    NSString *random = _codeField.text;
    NSString *pwd = _psdField.text;
    if (_phoneField.text.length==11&&pwd.length>=6&&random.length!=0&&[_psdField.text isEqualToString:_surePsdField.text])
    {
        NSString *urlStr = [NSString stringWithFormat:@"%@/sys/login/password/rest?mobile_no=%@&user_newPwd=%@&reg_num=%@",APIYIGOU,_phoneField.text,pwd,random];
        
        DYBaseRequest *request = [DYBaseRequest new];
        request.requestUrl = urlStr;
        [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
            NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
            NSLog(@"77777%@",allDic);
            
            
            [ViewFactory showSuccess:@"修改成功"];
            [self.navigationController popViewControllerAnimated:YES];
        } error:^(YTKBaseRequest *request) {
            NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
            NSLog(@"77777%@",allDic);
            [ViewFactory showInfoMsg:allDic[@"message"]];
        } failure:^(YTKBaseRequest *request) {
            [ViewFactory showErrorStrMsg:NETWORK_ERR];
        }];
    }
    else if (_phoneField.text.length!=11)
    {
        [ViewFactory showInfoMsg:@"请输入正确手机号"];
    }
    else if (random.length==0)
    {
        [ViewFactory showInfoMsg:@"请输入验证码"];
    }
    else if (pwd.length<6)
    {
        if (pwd.length==0) {
            [ViewFactory showInfoMsg:@"请输入密码"];
            return;
        }
        [ViewFactory showInfoMsg:@"请输入6位以上字母和数字密码"];
    }
    else
    {
        [ViewFactory showInfoMsg:@"两次输入密码不一致"];
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
